package com.hqj.controller;
import com.hqj.controller.vo.BlogVo;
import com.hqj.controller.vo.HomeAllVo;
import com.hqj.entity.Blog;
import com.hqj.service.BlogService;
import com.hqj.utils.ResultUtil;
import com.hqj.utils.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

/**
 * @author 黄啟军
 * @version 1.0
 * @date 2020/5/16/016
 */
@Api(tags = "BlogController")
@RestController
@RequestMapping("/blog")
@CrossOrigin
public class BlogController {

    @Resource
    private BlogService blogService;
    @ApiOperation("用户点赞")
    @GetMapping("/likesAll/{id}")
    public ResultVO<String> likesAll(@PathVariable("id") Long id){
        int all = blogService.likesAll(id);
        if (all>0){
            return ResultUtil.success("点赞成功");
        }
        return ResultUtil.error();
    }
    @ApiOperation("精华博客")
    @GetMapping("/essence")
    public ResultVO<List<Blog>> essence(@RequestParam(value = "current",defaultValue = "1") Integer current,
                                        @RequestParam(value = "size",defaultValue = "5")Integer size){
        List<Blog> essence = blogService.essence(current, size);
        if (essence.isEmpty()){
            return ResultUtil.error();
        }
        return ResultUtil.success(essence);
    }
    @ApiOperation("文本框搜索")
    @GetMapping("/seekAll/{seek}")
    public ResultVO<List<Blog>> seekAll(@PathVariable("seek") String seek){
        List<Blog> blogs = blogService.seekAll(seek);
        if (blogs.isEmpty()){
            return ResultUtil.error();
        }
        return ResultUtil.success(blogs);
    }
    @ApiOperation("根据ID查询")
    @GetMapping("/findById/{id}")
    public ResultVO<Blog> findById(@PathVariable("id") Long id){
        Blog byId = blogService.findById(id);
        if ( byId != null){
          return ResultUtil.success(byId);
        }
        return ResultUtil.error();
    }
    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public ResultVO<List<Blog>> findAll(){
        List<Blog> all = blogService.findAll();
        if (all.isEmpty()){
            return  ResultUtil.error();
        }
        return ResultUtil.success(all);
    }
    @ApiOperation("新增博客")
    @PostMapping("/saveSingle")
   public ResultVO<String> saveSingle(@RequestBody Blog blog){
        blog.setUserId(1L);
        int single = blogService.saveSingle(blog);
        if (single >0){
            return ResultUtil.success("新增成功");
        }
        return ResultUtil.error();
   }
   @ApiOperation("分页查询")
   @GetMapping("/pages")
   public ResultVO<BlogVo> pages(@RequestParam(value = "current",defaultValue = "1") Integer current,
                                 @RequestParam(value = "size",defaultValue = "5") Integer size){
       BlogVo pages = blogService.pages(current, size);
       if (pages != null){
           return ResultUtil.success(pages);
       }
       return ResultUtil.error();
   }
   @ApiOperation("分类查询")
   @GetMapping("/homeAll")
   public ResultVO<List<Object>> homeAll(){
       List<Object> homeAllVos = blogService.homeAll();
       if(homeAllVos.isEmpty()){
           return ResultUtil.error();
       }
       return ResultUtil.success(homeAllVos);
   }
}
